/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package ru.infosystem.client.dao;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import ru.infosystem.client.BDConnection;
import ru.infosystem.client.data.Faculty;
import ru.infosystem.client.data.Group;
import ru.infosystem.client.data.ServerException;
import ru.infosystem.client.model.GroupTableModel;
import ru.infosystem.client.task.Command;
import ru.infosystem.client.task.Task;

/**
 *
 * @author akoshi
 */
public class GroupDAO {

    public static GroupTableModel list(BDConnection connection, String faculty) throws IOException, ClassNotFoundException {

        Group[] groups = null;

        ObjectOutputStream out = connection.getObjOutputStream();

        ObjectInputStream in = connection.getObjInputStrem();

        out.writeObject(new Task(Command.LIST_GROUPS_OF_FACULTY, new Faculty(faculty)));


        Object o = null;
        try {
            o = in.readObject();
            if (o instanceof ServerException) {
                throw (ServerException) o;
            }
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(GroupDAO.class.getName()).log(Level.SEVERE, null, ex);
        }

        ArrayList<Group> list = (ArrayList<Group>) o;

        groups = new Group[list.size()];
        for (int i = 0; i < list.size(); i++) {
            groups[i] = list.get(i);
        }

        return new GroupTableModel(groups);
    }

    public static boolean deleteGroup(BDConnection connection, Group group) throws IOException {

        ObjectOutputStream out = connection.getObjOutputStream();

        ObjectInputStream in = connection.getObjInputStrem();

        out.writeObject(new Task(Command.DELETE_GROUP, group));
        Object o = null;
        try {
            o = in.readObject();
            if (o instanceof ServerException) {
                throw (ServerException) o;
            }
            
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(GroupDAO.class.getName()).log(Level.SEVERE, null, ex);
        }

        return ((Boolean) o).booleanValue();
    }

    public static boolean addGroup(BDConnection connection, Group group) throws IOException, ServerException {

        ObjectOutputStream out = connection.getObjOutputStream();

        ObjectInputStream in = connection.getObjInputStrem();

        out.writeObject(new Task(Command.ADD_GROUP, group));
        Object o = null;
        try {
            o = in.readObject();
            if (o instanceof ServerException) {
                throw (ServerException) o;
            }
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(GroupDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        //return in.readBoolean();
        return ((Boolean) o).booleanValue();
    }
}
